package com.wyz.easyexcel.service.impl;

import com.alibaba.excel.EasyExcel;
import com.wyz.easyexcel.entity.Student;
import com.wyz.easyexcel.entity.UploadStudent;
import com.wyz.easyexcel.handler.I18nCellWriteHandler;
import com.wyz.easyexcel.listener.UploadDataListener;
import com.wyz.easyexcel.service.UploadService;
import com.wyz.easyexcel.util.FileDownloadUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * @author wyz
 * @description: 上传业务实现
 * @date 2021/12/28 14:26
 */

@Slf4j
@Service
public class UploadServiceImpl implements UploadService {


    @Override
    public void upload(MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), UploadStudent.class, new UploadDataListener()).sheet().doRead();

    }

    @Override
    public void download() throws IOException {

//        List<String> excludeColumnFiledNames = null;
        //忽略字段
        List<String> excludeColumnFiledNames = List.of("birthday","age");
        List<I18nCellWriteHandler> i18nCellWriteHandlers = List.of(new I18nCellWriteHandler());
        FileDownloadUtils.downLoad(Student.class,"测试","模板",data(),i18nCellWriteHandlers,excludeColumnFiledNames);
    }

    private List<Student> data() {
       return List.of(
                new Student("张三", new Date(),20,"0"),
                new Student("李四", new Date(),21,"0"),
                new Student("王五", new Date(),22,"0"),
                new Student("美美", new Date(),18,"1"),
                new Student("妞妞", new Date(),22,"1"),
                new Student("妮妮", new Date(),25,"1")
                );
    }
}
